//
//  FingerprintIdentificationViewController.m
//  WZEfengAndEtong
//
//  Created by 李旭 on 16/7/21.
//  Copyright © 2016年 wanzhao. All rights reserved.
//

#import "FingerprintIdentificationViewController.h"
#import <LocalAuthentication/LocalAuthentication.h>
#import "Masonry.h"
#import "PassWordViewController.h"
@interface FingerprintIdentificationViewController ()
@property (nonatomic,weak) UIButton *fingerButton;
@end
static FingerprintIdentificationViewController *share;
@implementation FingerprintIdentificationViewController
+(instancetype)shareInstance
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [[FingerprintIdentificationViewController alloc]init];
    });
    return share;
}
+ (instancetype)alloc {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        share = [super alloc];
    });
    
    return share;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:[UIImage imageNamed:@"fin"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(begincertify) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(200);
        make.width.height.mas_equalTo(100);
        make.centerX.mas_equalTo(self.view);
    }];
    self.fingerButton = button;
    UILabel *label = [[UILabel alloc]init];
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = RGBCOLOR(44, 170, 253);;
    label.numberOfLines = 1;
    label.text = @"点击指纹进行指纹解锁";
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
        make.height.mas_equalTo(40);
        make.centerX.mas_equalTo(self.view);
        make.bottom.equalTo(button).offset(60);
    }];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self begincertify];
}
-(void)begincertify
{
    __weak typeof (self) weakself = self;
    LAContext* context = [[LAContext alloc] init];
    NSString* result = @"请验证您的指纹进入E蜂";
    [context evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics localizedReason:result reply:^(BOOL success, NSError *error) {
        if (success) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakself.navigationController popViewControllerAnimated:NO];
                [weakself.view removeFromSuperview];
                [weakself removeFromParentViewController];
            });
            
        }
        else
        {
            switch (error.code) {
                case LAErrorSystemCancel:
                {
                    break;
                }
                case LAErrorUserCancel:
                {
                    NSLog(@"Authentication was cancelled by the user");
                    //用户取消验证Touch ID
                    break;
                }
                case LAErrorUserFallback:
                {
                    NSLog(@"User selected to enter custom password");
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        PassWordViewController *pvc = [[PassWordViewController alloc]init];
                        [weakself presentViewController:pvc animated:YES completion:nil];
                        [weakself.navigationController popViewControllerAnimated:NO];
                        [weakself.view removeFromSuperview];
                        [weakself removeFromParentViewController];
                    }];
                    break;
                }
                default:
                {
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                        //其他情况，切换主线程处理
                    }];
                    break;
                }
            }
        }
    }];
    
}

@end
